﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.IO;
using teach.Panels;
using System.Reflection;

namespace teach
{

    public partial class TeachForm : Form
    {
        public delegate void QuestionAnsweredHandler(object answer);
        public event QuestionAnsweredHandler QuestionAnswered;
        public event EventHandler MainFormClosing;
        public event EventHandler NextQuestionAsked;
        public event EventHandler Timeout;
        public event EventHandler UserChanged;
        public delegate void UpdateUIDelegate(UIUPDATES update, object[] data);
        public UpdateUIDelegate myUpdateUI;

        private PanelBase m_currentPanel;
        private PanelBase m_previousPanel;
        private PlayerPanel m_playerPanel;
        private OperationPanel m_operationPanel;
        private ChoicesPanel m_choicesPanel;
        private AudioChoicesPanel m_audioChoicesPanel;
        private TextPanel m_textPanel;
        private WordPanel m_wordPanel;
        private AudioPanel m_audioPanel;
        private VideoPanel m_videoPanel;
        private DoubleNothinPanel m_doublePanel;

        /// <summary>
        /// Constructor
        /// </summary>
        public TeachForm()
        {
            InitializeComponent();
            myUpdateUI = new UpdateUIDelegate(this.UpdateUI);

            m_playerPanel = new PlayerPanel();
            m_playerPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            m_playerPanel.Location = new System.Drawing.Point(0, 0);
            m_playerPanel.Size = new System.Drawing.Size(1016, 519);
            m_playerPanel.MovieEnded += new EventHandler(m_playerPanel_MovieEnded);

            m_operationPanel = new OperationPanel();
            m_operationPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            m_operationPanel.Location = new System.Drawing.Point(0, 0);
            m_operationPanel.Size = new System.Drawing.Size(1016, 519);
            m_operationPanel.QuestionAnswered += new PanelBase.QuestionAnsweredHandler(PanelQuestionAnswered);
            m_operationPanel.NextClicked += new EventHandler(NextClicked);
            m_operationPanel.Timeout += new EventHandler(Manage_Timeout);

            m_textPanel = new TextPanel();
            m_textPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            m_textPanel.Location = new System.Drawing.Point(0, 0);
            m_textPanel.Size = new System.Drawing.Size(1016, 519);
            m_textPanel.QuestionAnswered += new PanelBase.QuestionAnsweredHandler(PanelQuestionAnswered);
            m_textPanel.NextClicked += new EventHandler(NextClicked);
            m_textPanel.Timeout += new EventHandler(Manage_Timeout);

            m_choicesPanel = new ChoicesPanel();
            m_choicesPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            m_choicesPanel.Location = new System.Drawing.Point(0, 0);
            m_choicesPanel.Size = new System.Drawing.Size(1016, 519);
            m_choicesPanel.QuestionAnswered += new PanelBase.QuestionAnsweredHandler(PanelQuestionAnswered);
            m_choicesPanel.NextClicked += new EventHandler(NextClicked);
            m_choicesPanel.Timeout += new EventHandler(Manage_Timeout);

            m_audioChoicesPanel = new AudioChoicesPanel();
            m_audioChoicesPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            m_audioChoicesPanel.Location = new System.Drawing.Point(0, 0);
            m_audioChoicesPanel.Size = new System.Drawing.Size(1016, 519);
            m_audioChoicesPanel.QuestionAnswered += new PanelBase.QuestionAnsweredHandler(PanelQuestionAnswered);
            m_audioChoicesPanel.NextClicked += new EventHandler(NextClicked);
            m_audioChoicesPanel.Timeout += new EventHandler(Manage_Timeout);
            m_audioChoicesPanel.SetAdditionalResourcePath(ConfigurationManager.AppSettings["ImagesAudioFolder"]);

            m_wordPanel = new WordPanel();
            m_wordPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            m_wordPanel.Location = new System.Drawing.Point(0, 0);
            m_wordPanel.Size = new System.Drawing.Size(1016, 519);
            m_wordPanel.QuestionAnswered += new PanelBase.QuestionAnsweredHandler(PanelQuestionAnswered);
            m_wordPanel.NextClicked += new EventHandler(NextClicked);
            m_wordPanel.Timeout += new EventHandler(Manage_Timeout);
            m_wordPanel.SetAdditionalResourcePath(Constants.FOLDER_QUESTIONS_IMAGES);

            m_audioPanel = new AudioPanel();
            m_audioPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            m_audioPanel.Location = new System.Drawing.Point(0, 0);
            m_audioPanel.Size = new System.Drawing.Size(1016, 519);
            m_audioPanel.QuestionAnswered += new PanelBase.QuestionAnsweredHandler(PanelQuestionAnswered);
            m_audioPanel.NextClicked += new EventHandler(NextClicked);
            m_audioPanel.Timeout += new EventHandler(Manage_Timeout);
            m_audioPanel.SetAdditionalResourcePath(Constants.FOLDER_QUESTIONS_AUDIO);

            m_videoPanel = new VideoPanel();
            m_videoPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            m_videoPanel.Location = new System.Drawing.Point(0, 0);
            m_videoPanel.Size = new System.Drawing.Size(1016, 519);
            m_videoPanel.QuestionAnswered += new PanelBase.QuestionAnsweredHandler(PanelQuestionAnswered);
            m_videoPanel.NextClicked += new EventHandler(NextClicked);
            m_videoPanel.Timeout += new EventHandler(Manage_Timeout);
            m_videoPanel.SetAdditionalResourcePath(Constants.FOLDER_QUESTIONS_VIDEO);

            m_doublePanel = new DoubleNothinPanel();
            m_doublePanel.Dock = System.Windows.Forms.DockStyle.Fill;
            m_doublePanel.Location = new System.Drawing.Point(0, 0);
            m_doublePanel.Size = new System.Drawing.Size(1016, 519);
            m_doublePanel.QuestionAnswered += new PanelBase.QuestionAnsweredHandler(PanelQuestionAnswered);

        }

        void m_videoPanel_FullScreenEvent(object sender, EventArgs e)
        {
            if (this.FormBorderStyle == System.Windows.Forms.FormBorderStyle.None)
            {
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.WindowState = FormWindowState.Normal;
                panelTitle.Visible = true;
            }
            else
            {
                panelTitle.Visible = false;
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;
            }

        }

        private void m_playerPanel_MovieEnded(object sender, EventArgs e)
        {
            if (this.FormBorderStyle == System.Windows.Forms.FormBorderStyle.None)
            {
                this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
                this.WindowState = FormWindowState.Normal;
            }
            panelTitle.Visible = true;
            NextQuestionAsked(sender, e);
        }

        private void Manage_Timeout(object sender, EventArgs e)
        {
            Timeout(sender, e);
        }

        private void NextClicked(object sender, EventArgs e)
        {
            NextQuestionAsked(sender, e);
        }

        private void PanelQuestionAnswered(object answer)
        {
            QuestionAnswered(answer);
        }


        /// <summary>
        /// Switch the panel to a UI
        /// </summary>
        /// <param name="UI"></param>
        public void SwitchPanel(string UI)
        {
            this.panelMain.SuspendLayout();
            this.panelMain.Controls.Clear();
            switch (UI)
            {
                case "OPERATION":
                    this.panelMain.Controls.Add(m_operationPanel);
                    m_currentPanel = m_operationPanel;
                    this.AcceptButton = m_operationPanel.ButtonAnswer;
                    break;
                case "TEXT":
                    this.panelMain.Controls.Add(m_textPanel);
                    m_currentPanel = m_textPanel;
                    this.AcceptButton = m_textPanel.ButtonAnswer;
                    break;
                case "IMAGES":
                    this.panelMain.Controls.Add(m_wordPanel);
                    m_currentPanel = m_wordPanel;
                    this.AcceptButton = m_wordPanel.ButtonAnswer;
                    break;
                case "MIXED":
                    this.panelMain.Controls.Add(m_wordPanel);
                    m_currentPanel = m_wordPanel;
                    break;
                case "CHOICES":
                    this.panelMain.Controls.Add(m_choicesPanel);
                    m_currentPanel = m_choicesPanel;
                    break;
                case "AUDIO":
                    this.panelMain.Controls.Add(m_audioPanel);
                    m_currentPanel = m_audioPanel;
                    this.AcceptButton = m_audioPanel.ButtonAnswer;
                    break;
                case "AUDIOCHOICES":
                    this.panelMain.Controls.Add(m_audioChoicesPanel);
                    m_currentPanel = m_audioChoicesPanel;
                    break;
                case "MOVIE":
                    this.panelMain.Controls.Add(m_playerPanel);
                    m_currentPanel = m_playerPanel;
                    break;
                case "VIDEO":
                     m_audioPanel.StopAudio();
                    m_videoPanel.StopVideo();
                    this.panelMain.Controls.Add(m_videoPanel);
                    m_currentPanel = m_videoPanel;
                    this.AcceptButton = m_videoPanel.ButtonAnswer;
                    break;
                case "DOUBLE":
                    this.panelMain.Controls.Add(m_doublePanel);
                    m_currentPanel = m_doublePanel;
                    break;
            }
            this.panelMain.ResumeLayout();
            this.panelMain.Refresh();
        }

        /// <summary>
        /// Update the UI
        /// </summary>
        private void UpdateUI(UIUPDATES update, object[] data)
        {
            try
            {
                switch (update)
                {
                    case UIUPDATES.USER_BUTTON:
                        //btnChangeUser.Image = data[0] as Image;
                        //btnChangeUser.Refresh();
                        break;
                    case UIUPDATES.TITLE:
                        lblTitle.Text = data[0] as string;
                        lblTitle.Refresh();
                        break;
                    case UIUPDATES.LABELVIDEO:
                        int val = (int)data[0];
                        lblVideos.Visible = true;
                        lblVideos.Text = val + " video";
                        if (val > 1)
                        {
                            lblVideos.Text = lblVideos.Text + "s";
                        }
                        lblVideos.Refresh();
                        break;
                    case UIUPDATES.PANELSWITCH:
                        SwitchPanel(data[0] as string);
                        break;
                    case UIUPDATES.STATUS:
                        bool success = (bool)data[0];
                        if (success)
                        {
                            lblTitle.Text = "Bravo!";
                        }
                        else
                        {
                            lblTitle.Text = "Oups...";
                        }
                        m_currentPanel.SetStatusImage(success, data[1] as Image);
                        break;
                    case UIUPDATES.CINEMA:
                        m_audioPanel.StopAudio();
                        m_videoPanel.StopVideo();
                        m_previousPanel = m_currentPanel;
                        lblTitle.Text = "Félicitations!!!";
                        SwitchPanel("MOVIE");
                        PlayerPanel cinemaPanel = m_currentPanel as PlayerPanel;
                        panelTitle.Visible = false;
                        cinemaPanel.StartVideo(data[0] as string);
                        break;
                    case UIUPDATES.SETQUESTION:
                        m_currentPanel.SetQuestion(data[0] as Question);
                        this.ActiveControl = m_currentPanel.GetAnswerControl();
                        break;
                    case UIUPDATES.STATS:
                        this.Text = "PROF " + Assembly.GetEntryAssembly().GetName().Version + "   (" + (int)data[0] + " bonnes réponses sur " + (int)data[1] + ")";
                        if ((int)data[2] == 1)
                        {
                            this.Text += "                  " + (int)data[3] + " DE SUITE! / " + (int)data[2] + " vidéo";
                        }
                        else
                        {
                            this.Text += "                  " + (int)data[3] + " DE SUITE! / " + (int)data[2] + " vidéos";
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                if (data[0] != null)
                {
                    TeachController.LogError("UpdateUI(" + update.ToString() + ", " +data[0].ToString()+")", ex);
                }
                else
                {
                    TeachController.LogError("UpdateUI(" + update.ToString() + ", null)", ex);
                }
                
                throw ex;
            }
        }

        /// <summary>
        /// Override the onLoad event manager
        /// </summary>
        protected override void OnLoad(EventArgs args)
        {
            base.OnLoad(args);
            Application.Idle += new EventHandler(OnLoaded);
        }

        /// <summary>
        /// Manage the OnLoaded event.
        /// Throws the MainFormLoaded event for the manager
        /// </summary>
        private void OnLoaded(object sender, EventArgs args)
        {
            Application.Idle -= new EventHandler(OnLoaded);
        }


        private void btnChangeUser_Click(object sender, EventArgs e)
        {
            UserChanged(this, null);
        }

        private void lblUserName_Click(object sender, EventArgs e)
        {
            UserChanged(this, null);
        }

        private void TeachForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            MainFormClosing(this, null);
        }


    }
}
